import cv2
import json
import numpy as np

# pos calib
img = cv2.imread("fram_vr_resized.jpg")
pts_vr = np.float32([[401,245],[409,317],[329,244],[309,290]])
pts_ir = np.float32([[484,137],[499,323],[311,129],[262,247]])
M = cv2.getPerspectiveTransform(pts_vr, pts_ir)
print("M: ", M)
dst = cv2.warpPerspective(img, M, (320, 240))
#cv2.imwrite("dst.jpg", dst)

#test
M = np.array(M)
p0 = np.array([149, 156, 1])
p1 = np.dot(M, p0)
print("sssssssssssssssssddddddddddd:", p1)

M = M.tolist()

# temperature calib
pixlum = [48, 225]#, 134, 129]
temperature = [25.0, 36.5]#, 35.7, 35.2]
z1 = np.polyfit(pixlum, temperature, 1)
p1 = np.poly1d(z1)
print("z1: ", z1) # [1.   1.499]
print("p1: ", p1) # 1 x + 1.499
z1 = z1.tolist()


# save to file
dict = {}
dict.update({"posCalibM":M})
dict.update({"temperatureCalib":z1})
result = json.dumps(dict)
with open("calibData.dat", "w", encoding="utf-8") as f:
    f.write(result)
